
var lang = "en-US";


function dateToTS(date) {
    return date.valueOf();
}

function tsToDate(ts) {
    var d = new Date(ts);
    if(time_unit_type =='hour'){
        // console.log(moment(d).format('D日 HH:mm'));
        return moment(d).format('D日 HH:mm');
    }else{
        return moment(d).format('YYYY-MM-DD');
    }
}

function show_gante_bar(){
    var taskid = current_opt_task_id;
    var domid = current_opt_dom_id;
    console.log(domid);
    hide_toolbar();
    layer.alert("请到下面的网址查看：<br><a href=/gante/" + taskid + " target=_BLANK>http://thinktodo.cn/gante/" + taskid + "</a>");
    
    //下行用于在本页面打开进度条
    // make_gante_bar(domid);
}

function expend_gante_bar(){
	get_task_start_end();
}

function get_task_start_end() {
    var url;
    var str;
    // var rootTaskId = $("#parm").attr("data-rootTaskId");
    url = "/index.php/tasktree/getallchild_start_end/" + rootTaskId;

    var items;
    $.getJSON(url,
        function (dataGet) {
            //			console.log(dataGet);
            if (dataGet.data) {
                //					console.log(dataGet.data);
                items = dataGet.data;
                console.log(items);
                $.each(items, function (idx, item) { //生成所有任务，先放到任务挂装区
                    if(item.start){
                        min = moment(item.start).valueOf();
                    }else{
                        min = moment().subtract(1,'months').valueOf();
                    }
                    if(item.end){
                        max = moment(item.end).valueOf();
                    }else{
                        max = moment().add(1,'months').valueOf();
                    }
                    
                    // console.log(moment(min).format('YYYY-MM-DD'));
                    // console.log(moment(max).format('YYYY-MM-DD'));
                });

            };
            make_gante_calendar('month');
            // make_gante_calendar('week');
            // var domid=$("#gante_view>div.task").attr("id");
            make_gante_bar();
            // $("#"+domid+" div.task").show();


        }
    );
}			

function make_gante_calendar(unit_type){
    //time_unit,网格的时间单位month,week,day,hour
    var unit_types = unit_type + "s";
    var head_format_type = {
        'month':'M',
        'week':'w',
        'day':'D',
        'hour':'H'
    };
    console.log('min:'+moment(min).format('YYYY-MM-DD'));
    console.log('max:'+moment(max).format('YYYY-MM-DD'));

    var micro_seconds_persent = moment(max).diff(moment(min))/100;
    var ms = moment(max).diff(moment(min), unit_types);
    var htmlstr,theadstr,tbodystr;
    var m,unit_time,unit_start,unit_end, unit_long;
    htmlstr="<table id='' class=" + unit_type + ">";
    // console.log(moment(min).format("YYYY-MM-DD"));
    unit_long = moment(min).endOf(unit_type).diff(moment(min));
    // console.log(unit_long);
    theadstr = "<td style='width:" + unit_long/micro_seconds_persent +"%' >"+moment(min).format(head_format_type[unit_type])+"</td>";
    tbodystr = "<td></td>";
    for (i = 1; i <= ms; i++) { 
        unit_time = moment(min).add(i,unit_types);
        console.log(unit_time.format("YYYY-MM-DD"));
        // console.log(unit_time.daysInMonth());
        unit_start = moment(unit_time).startOf(unit_type);
        unit_end = moment(unit_time).endOf(unit_type);
        unit_long = unit_end.diff(unit_start);
        // unit_long = unit_time.endOf(unit_type).diff(unit_time.startOf(unit_type));

        // if(unit_type =="month"){
        //     unit_long = unit_time.daysInMonth()*24*3600*1000;

        // }else{
        //     unit_long = unit_time.daysInMonth()*24*3600*1000;
        // }
        theadstr += ("<td style='width:"+ unit_long/micro_seconds_persent +"%' >" + unit_time.format(head_format_type[unit_type]) +"</td>");
        tbodystr += "<td></td>"
     }

    //  unit_long = moment(max).diff(moment(max).startOf(unit_type));
    //  theadstr += "<td style='width:" + unit_long/micro_seconds_persent +"%' >"+moment(max).format("M")+"</td>";
    //  tbodystr += "<td></td>";

     htmlstr += "<thead>" + theadstr +"</thead>";
     htmlstr += "<tbody>" + tbodystr +"</tbody>" ;
     htmlstr += "</table>";
    //  console.log(htmlstr);
     $("#gante_head table").remove();
     $("#gante_head").append(htmlstr);	
}

function change_unit_type(new_unit_type){
    time_unit_type = new_unit_type;
    make_gante_calendar(new_unit_type);
    // make_gante_bar();
}


function make_gante_bar() {
    var domid=$("#gante_view>div.task").attr("id");
    $("#"+domid+" div.task").show();
// 清除原进度条
    $("#" + domid + " input.gante_bar").remove();
    $("#" + domid + " span.irs").remove();

    $("#" + domid + " div.task a.btn_for_toolbar").after('<input class="gante_bar js-range-slider"></input>');

    // var min = dateToTS(new Date());
    // var max = dateToTS(new Date());
    console.log(moment(min).format('YYYY-MM-DD HH:mm:ss'));
    console.log(moment(max).format('YYYY-MM-DD HH:mm:ss'));

    $(".gante_bar.js-range-slider").each(function (i) {
        // console.log($(this).closest("div.task").attr("data-start"));

        var start = $(this).closest("div.task").attr("data-start");
        var end = $(this).closest("div.task").attr("data-end");

        console.log(start+"----"+end);
        if (start === 'null' && end === 'null') {
            $(this).attr("data-from", max);
            $(this).attr("data-to", max);
        } else {
            if (start === 'null') {
                $(this).attr("data-from", max);
            } else {

                $(this).attr("data-from", dateToTS(new Date(start)));
            }
            if (end === 'null') {
                $(this).attr("data-to", dateToTS(new Date(start)));
            } else {

                $(this).attr("data-to", dateToTS(new Date(end)));
            }
        }
    });


    $(".gante_bar.js-range-slider").each(function (i) {
        $(this).ionRangeSlider({
            type: "double",
            hide_min_max: true,
            hide_from_to: false,
            min: min,
            max: max,
            // from: dateToTS(new Date($(this).closest("div.task").attr("data-start"))),
            // to:   dateToTS(new Date($(this).closest("div.task").attr("data-end"))),
            prettify: tsToDate,
            onFinish: function (data) {
                console.dir(data);
                var new_start = moment(data.from).format("YYYY-MM-DD HH:mm:ss");
                var new_end = moment(data.to).format("YYYY-MM-DD HH:mm:ss");

                var thetaskid = $(data.input).closest("div.task").attr("data-taskid");

                $.post("/index.php/task/change",
                    {
                        id: thetaskid,
                        parm1: 'start_date',
                        value1: new_start,
                    },
                    function (dataGet, status) {
                        if (dataGet) {
                            layer.msg('服务端开始日程修改成功！', { time: 2000 });
                        } else {
                            alert("服务器修改失败！\n" + dataGet.status + dataGet.message);
                        }
                    }, "json");
                $.post("/index.php/task/change",
                    {
                        id: thetaskid,
                        parm1: 'end_date',
                        value1: new_end,
                    },
                    function (dataGet, status) {
                        if (dataGet) {
                            layer.msg('服务端结束日程修改成功！', { time: 2000 });
                        } else {
                            alert("服务器修改失败！\n" + dataGet.status + dataGet.message);
                        }
                    }, "json");
            }
        });
    });

$(".js-irs-0").on("finish", function () {

    var $inp = $(this);
    var from = $inp.prop("value"); // reading input value
    var from2 = $inp.data("from"); // reading input data-from attribute

    console.log(from, from2); // FROM value


});



}